<?php 

require ("../lib/upload.php");
require ("../lib/imaging.php");
if($_POST['code']=='addnew'){
	if($_FILES['productimage']['size']){
		$x=	uploadimage_photo('productimage');
		$a=array();
		
		$a['image']				=$x['image'];
		$a['normal_image']		=$x['normal_image'];
		$a['small_image']		=$x['small_image'];
		$a['name']				=clean_value($_POST['name']);
		$a['id_product']		=intval($_POST['id_product_photo']);
		
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO image_news (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		$id=intval(mysql_insert_id());
		if(file_exists("../".$CONFIG['upload_image_path'].$x['small_image'])){
			$size=getimagesize("../".$CONFIG['upload_image_path'].$x['small_image']);
			if($size[0]>=$size[1]){
				echo '<div class="cell_product" id="image_news_'.$id.'"><div id="control"><a href="#"  onclick="deleteimage_product(\''.$id.'\'); return false;">Xóa</a></div><img src="../'.$CONFIG['upload_image_path'].$x['small_image'].'" width="130" id="'.$id.'" /></div>';	
			}else{
				echo '<div class="cell_product" id="image_news_'.$id.'"><div id="control"><a href="#" onclick="deleteimage_product(\''.$id.'\'); return false;">Xóa</a></div><img src="../'.$CONFIG['upload_image_path'].$x['small_image'].'" height="130" id="'.$id.'"/></div>';	
			}
			
		}
		
	}
}elseif($_GET['code']=='delete'){
	$id=intval($_GET['id']);
	$sql="SELECT * FROM image_product WHERE id=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		deleteimage($rs['image'],$rs['normal_image'],$rs['small_image']);
	}
	$DB->query("DELETE FROM image_product WHERE id=".$id);
}
if($_GET['code']=='load'){
	$id=intval($_GET['id']);
	$sql="SELECT * FROM image_product WHERE id_product=$id";
	$db=$DB->query($sql);
	$str='';
	while($x=mysql_fetch_array($db)){
		if(file_exists("../".$CONFIG['upload_image_path'].$x['small_image'])){
			$size=getimagesize("../".$CONFIG['upload_image_path'].$x['small_image']);
			if($size[0]>=$size[1]){
				echo '<div class="cell_product" id="image_product_'.$x['id'].'"><div id="control"><a href="#"  onclick="deleteimage_product(\''.$x['id'].'\'); return false;">Xóa</a></div><img src="../'.$CONFIG['upload_image_path'].$x['small_image'].'" width="130" id="'.$x['id'].'" /></div>';	
			}else{
				echo '<div class="cell_product" id="image_product_'.$x['id'].'"><div id="control"><a href="#" onclick="deleteimage_product(\''.$x['id'].'\'); return false;">Xóa</a></div><img src="../'.$CONFIG['upload_image_path'].$x['small_image'].'" height="130" id="'.$x['id'].'"/></div>';	
			}
			
		}	
	}
	echo $str;
}
function uploadimage_photo($inputname,$thumnail='yes',$dich=''){
	global $CONFIG,$_FILES,$tpl,$category,$par_page;
	$a=array();
	if($dich==''){
		$dich=$CONFIG['root_path'].$CONFIG['upload_image_path'];
	}else {
		$dich=$dich;
	}
	if($category==''){
		$category=$par_page;
	}
	if ($_FILES[$inputname]['size']){
		$in_image=get_new_file_name($_FILES[$inputname]['name'],clean_url(get_file_name($_FILES[$inputname]['name'])));
		$file_upload=new Upload($dich,'jpg,jpge,gif,png,bmp');
		if ($file_upload->upload_file($inputname,2,$in_image))
		{
			//Da upload thanh cong
			//Tao thumbnail
			$a['image']=$file_upload->file_name;
			if($CONFIG['watermark_image']){
				$watermark = WideImage::load($CONFIG['root_path'].$CONFIG['upload_image_path'].$CONFIG['watermark_image']);
				$base = WideImage::load($dich.$a['image']);
				$result = $base->merge($watermark, "right - 10", "bottom - 10", 100);
			    $result->saveToFile($dich.$a['image']);
			}elseif($CONFIG['watermark_text']){
				 $img = WideImage::load($dich.$a['image']);
				 $canvas = $img->getCanvas();
				
				 $canvas->useFont('VARISON.TTF', 13, $img->allocateColor(255, 255, 255));
				 $canvas->writeText("right - 9", "top + 11", $CONFIG['watermark_text']);
				 $canvas->useFont('VARISON.TTF', 13, $img->allocateColor(9, 171, 25));
				 $canvas->writeText("right - 10", "top + 10", $CONFIG['watermark_text']);
				 $img->saveToFile($dich.$a['image']);	
				
			}
		  	deleteimage($dich.$a['image']);
			if($thumnail=='yes'){
				$thumbnail=create_thumb($dich, $file_upload->file_name);
				if ($thumbnail)
				{
					$a['small_image']=$thumbnail['thumb'];
					$a['normal_image']=$thumbnail['normal'];
				}
				else
				{
					$msg.="Kh&#244;ng t&#7841;o &#273;&#432;&#7907;c &#7843;nh thumbnail ! Xem l&#7841;i &#273;&#7883;nh d&#7841;ng file !<br>";
				}
			}
		}
		else
		{
			$msg.=$file_upload->get_upload_errors()."<br>";
		}			
		$a['msg']=$msg;
	}
	return $a;
}


?>
